﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MediaManager
{
    public partial class SearchManage : Form
    {
        private SearchManage()
        {
            InitializeComponent();
        }
        public SearchManage(string userName)
        {
            InitializeComponent();
            _userName = userName;
        }
        private string _userName = "";
        private Manage _manage = null;
        public SearchManage(Manage manage)
        {
            InitializeComponent();
            _manage = manage;
        }
        private void Init()
        {
            txtName.Text = "";
            dtBeginDate.Value = DateTime.Now;
            dtEndDateTime.Value = DateTime.Now;
            txtBingming.Text = "";
            dtChuyuanshijian.Value = DateTime.Now;
            dtZhuyuanshijian.Value = DateTime.Now;
            txtjianbing.Text = "";
            txtZhubing.Text = "";
            txtHebingzheng.Text = "";
            txtZhuZhiYiShi.Text = "";
            txtZhuankejiancha.Text = "";
            dataGridView1.DataSource = null;
            dataGridView1.Refresh();

        }
        private void btnInit_Click(object sender, EventArgs e)
        {
            Init();
        }
        private List<BaseTable> listBaseTable = null;
        private void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                IQueryable<BaseTable> iQueryable = null;
                bool flag = true;
                if (txtName.Text.Length > 0)
                {
                    flag = false;
                    iQueryable = _dataContext.BaseTable.Where(p => p.姓名.Contains(txtName.Text));
                }
                if (dtBeginDate.Value.ToShortDateString() != dtEndDateTime.Value.ToShortDateString() && dtBeginDate.Value < dtEndDateTime.Value)
                {
                    flag = false;
                    if (null != iQueryable)
                    {
                        iQueryable = iQueryable.Where(p => p.日期 >= dtBeginDate.Value && p.日期 < dtEndDateTime.Value);
                    }
                    else
                    {
                        iQueryable = _dataContext.BaseTable.Where(p => p.日期 >= dtBeginDate.Value && p.日期 < dtEndDateTime.Value);
                    }
                }

                //第二列
                if (txtBingming.Text.Length > 0)
                {
                    flag = false;
                    if (null != iQueryable)
                    {
                        iQueryable = iQueryable.Where(p => p.中医病名.Contains(txtBingming.Text));
                    }
                    else
                    {
                        iQueryable = _dataContext.BaseTable.Where(p => p.中医病名.Contains(txtBingming.Text));
                    }
                }
                if (this.dtChuyuanshijian.Value.ToShortDateString() != this.dtZhuyuanshijian.Value.ToShortDateString() && dtZhuyuanshijian.Value < dtChuyuanshijian.Value)
                {
                    flag = false;
                    if (null != iQueryable)
                    {
                        iQueryable = iQueryable.Where(p => p.住院时间 >= dtZhuyuanshijian.Value && p.出院时间 <= dtChuyuanshijian.Value);
                    }
                    else
                    {
                        iQueryable = _dataContext.BaseTable.Where(p => p.住院时间 >= dtZhuyuanshijian.Value && p.出院时间 <= dtChuyuanshijian.Value);
                    }
                }
                if (txtjianbing.Text.Length > 0)
                {
                    flag = false;
                    if (null != iQueryable)
                    {
                        iQueryable = iQueryable.Where(p => p.兼病.Contains(txtjianbing.Text));
                    }
                    else
                    {
                        iQueryable = _dataContext.BaseTable.Where(p => p.兼病.Contains(txtjianbing.Text));
                    }
                }
                if (txtZhubing.Text.Length > 0)
                {
                    flag = false;
                    if (null != iQueryable)
                    {
                        iQueryable = iQueryable.Where(p => p.主病.Contains(txtZhubing.Text));
                    }
                    else
                    {
                        iQueryable = _dataContext.BaseTable.Where(p => p.主病.Contains(txtZhubing.Text));
                    }
                }
                if (txtHebingzheng.Text.Length > 0)
                {
                    flag = false;
                    if (null != iQueryable)
                    {
                        iQueryable = iQueryable.Where(p => p.合并症.Contains(txtHebingzheng.Text));
                    }
                    else
                    {
                        iQueryable = _dataContext.BaseTable.Where(p => p.合并症.Contains(txtHebingzheng.Text));
                    }
                }
                if (txtZhuZhiYiShi.Text.Length > 0)
                {
                    flag = false;
                    if (null != iQueryable)
                    {
                        iQueryable = iQueryable.Where(p => p.主治医师.Contains(txtZhuZhiYiShi.Text));
                    }
                    else
                    {
                        iQueryable = _dataContext.BaseTable.Where(p => p.主治医师.Contains(txtZhuZhiYiShi.Text));
                    }
                }
                if (txtZhuankejiancha.Text.Length > 0)
                {
                    flag = false;
                    if (null != iQueryable)
                    {
                        iQueryable = iQueryable.Where(p => p.专科检查.Contains(txtZhuankejiancha.Text));
                    }
                    else
                    {
                        iQueryable = _dataContext.BaseTable.Where(p => p.专科检查.Contains(txtZhuankejiancha.Text));
                    }
                }
                if (flag)
                {
                    iQueryable = _dataContext.BaseTable.Where(p => true);
                }
                if (null != iQueryable)
                {
                    listBaseTable = iQueryable.ToList();
                    dataGridView1.DataSource = listBaseTable;
                    dataGridView1.Refresh();
                }
                else
                {
                    dataGridView1.DataSource = null;
                    dataGridView1.Refresh();
                }
            }
            catch (Exception ex)
            {
                GlobalBase.CatchException.DealWithException(ex);
                GlobalBase.Global.Instance.ShowMessage(ex.Message);
            }
        }
        private DataClasses1DataContext _dataContext = new DataClasses1DataContext();
        private void SearchManage_Load(object sender, EventArgs e)
        {
            Init();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        public void Print(string userName)
        {
            if (dataGridView1.DataSource == null)
            {
                GlobalBase.Global.Instance.ShowMessage("请先查询数据！");
                return;
            }
            int j = dataGridView1.CurrentRow.Index;
            if (j < 0)
            {
                GlobalBase.Global.Instance.ShowMessage("请先选中要查看的数据行！");
                return;
            }
            Manage m;
            if (null != listBaseTable && listBaseTable.Count > 0)
            {
                m = new Manage(userName);
                BaseTable baseTable = listBaseTable.ElementAt(j);
                m.UpdateMediaData(baseTable.编号);
                m.Print();
            }
            else
            {
                throw new Exception("提取数据错误！");
            }   
        }
        public void btnDel_Click(object sender, EventArgs e)
        {
            try
            {
                if (dataGridView1.DataSource == null)
                {
                    GlobalBase.Global.Instance.ShowMessage("请先查询数据！");
                    return;
                }
                int j = dataGridView1.CurrentRow.Index;
                if (j < 0)
                {
                    GlobalBase.Global.Instance.ShowMessage("请先选中要删除的数据行！");
                    return;
                }
                if (null != listBaseTable && listBaseTable.Count > 0)
                {
                    BaseTable baseTable = listBaseTable.ElementAt(j);
                    string s = "确实删除编号为：{0}，姓名为：{1}的这条数据吗？";
                    s = string.Format(s, baseTable.编号, baseTable.姓名);
                    if (MessageBox.Show(s, "信息", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        DelFromID(baseTable.编号);
                        string sss = string.Format(StructDayInfo.删除病例, baseTable.姓名, DateTime.Now);
                        InserDayInfo(sss);
                        btnSearch_Click(sender, e);
                    }
                }
                else
                {
                    throw new Exception("提取数据错误！");
                }

            }
            catch (Exception ex)
            {
                GlobalBase.CatchException.DealWithException(ex);
                GlobalBase.Global.Instance.ShowMessage(ex.Message);
            }
        }
        private void DelFromID(int id)
        {
            BaseTable baseTable = _dataContext.BaseTable.Where(p => p.编号 == id).Single<BaseTable>();
            _dataContext.BaseTable.DeleteOnSubmit(baseTable);
            DongMaiSystem dongMaiSystem = _dataContext.DongMaiSystem.Where(p => p.id == id).Single<DongMaiSystem>();
            _dataContext.DongMaiSystem.DeleteOnSubmit(dongMaiSystem);
            JingMaiSystem jingMaiSystem = _dataContext.JingMaiSystem.Where(p => p.id == id).Single<JingMaiSystem>();
            _dataContext.JingMaiSystem.DeleteOnSubmit(jingMaiSystem);
            JuBu juBu = _dataContext.JuBu.Where(p => p.ID == id).Single<JuBu>();
            _dataContext.JuBu.DeleteOnSubmit(juBu);
            ZhuanKe zhuanKe = _dataContext.ZhuanKe.Where(p => p.id == id).Single<ZhuanKe>();
            _dataContext.ZhuanKe.DeleteOnSubmit(zhuanKe);
            _dataContext.SubmitChanges();
        }

        public void btnUpdate_Click(object sender, EventArgs e)
        {
            Global.Instance.ShowFormID(dataGridView1, listBaseTable, _manage);
            //try
            //{
            //    if (dataGridView1.DataSource == null)
            //    {
            //        GlobalBase.Global.Instance.ShowMessage("请先查询数据！");
            //        return;
            //    }
            //    int j = dataGridView1.CurrentRow.Index;
            //    if (j < 0)
            //    {
            //        GlobalBase.Global.Instance.ShowMessage("请先选中要查看的数据行！");
            //        return;
            //    }
            //    if (null != listBaseTable && listBaseTable.Count > 0)
            //    {
            //        BaseTable baseTable = listBaseTable.ElementAt(j);
            //        string s = "确实查看编号为：{0}，姓名为：{1}的这条数据吗？";
            //        s = string.Format(s, baseTable.编号, baseTable.姓名);
            //        if (MessageBox.Show(s, "信息", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            //        {
            //            if (null != _manage && !_manage.IsDisposed)
            //            {
            //                _manage.Show();
            //                _manage.Focus();
            //            }
            //            else
            //            {
            //                _manage = new Manage(Global.Instance.UserName);
            //                _manage.MdiParent = Global.Instance.MdiManage;
            //                _manage.Show();
            //                _manage.Focus();
            //            }
            //            UpdateFromID(baseTable.编号);
            //            //btnClose_Click(sender, e);
            //        }
            //    }
            //    else
            //    {
            //        throw new Exception("提取数据错误！");
            //    }

            //}
            //catch (Exception ex)
            //{
            //    GlobalBase.CatchException.DealWithException(ex);
            //    GlobalBase.Global.Instance.ShowMessage(ex.Message);
            //}
        }
        private void UpdateFromID(int id)
        {
            if (null != onUpdateData)
            {
                onUpdateData(id);
            }
        }
        public UpdateData onUpdateData = null;

        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            btnUpdate_Click(sender, e);
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
        private void InserDayInfo(string dInfo)
        {
            DataClasses2DataContext dataContext = new DataClasses2DataContext();
            DayInfo dayInfo = new DayInfo();
            dayInfo.id = dataContext.GetDayInfoIndex();
            dayInfo.name = _userName;
            dayInfo.操作 = dInfo;
            dayInfo.日期 = DateTime.Now;
            dataContext.DayInfo.InsertOnSubmit(dayInfo);
            dataContext.SubmitChanges();

        }

        private void txtZhuZhiYiShi_TextChanged(object sender, EventArgs e)
        {

        }

    }
    public delegate void UpdateData(int id);
}
